package me.zhengjie.modules.member.rest;

import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.service.member.domain.Employee;
import com.qiaofang.applet.service.member.service.AppointmentService;
import com.qiaofang.applet.service.member.service.EmployeeService;
import com.qiaofang.applet.service.member.service.dto.AppointmentEmpAdminQueryDto;
import com.qiaofang.applet.service.member.service.dto.EmployeeDetailDto;
import com.qiaofang.applet.service.member.service.dto.EmployeeQueryDto;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.property.service.CommonService;
import com.qiaofang.applet.service.property.service.dto.EmployeeReplyEditDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.member.vo.EmployeeSavDto;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * Created by IDEA.
 *
 * @author QF2019
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "其它：经纪人管理")
@RequestMapping("/api/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private CorporationService corporationService;

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommonService commonService;

    @GetMapping
    @ApiOperation(value = "经纪人管理列表", notes = "经纪人管理列表")
    public ResponseEntity<Page<EmployeeDetailDto>> list(EmployeeQueryDto queryDto, Pageable pageable) {
        queryDto.setCorporationCode(SecurityUtils.getCorporationCode());
        queryDto.setCityCodes(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        Page<Employee> page = employeeService.findAllByParams(queryDto, pageable);
        List<EmployeeDetailDto> list = new LinkedList<>();
        if (!CollectionUtils.isEmpty(page.getContent())) {
            Map<String, String> cityMap = cityCompanyService.getCityMap(queryDto.getCorporationCode());
            for (Employee e : page.getContent()) {
                EmployeeDetailDto employeeDetailDto = employeeService.domainToDetailDto(e, cityMap);
                list.add(employeeDetailDto);
            }
        }
        return ResponseEntity.ok(PageableExecutionUtils.getPage(list, pageable, () -> page.getTotalElements()));
    }

    @PostMapping
    @ApiOperation(value = "保存经纪人主推信息与标签信息", notes = "保存经纪人主推信息与标签信息")
    public ResponseEntity<Object> update(@RequestBody EmployeeSavDto employeeSavDto) {
        Employee employee = new Employee();
        employee.setId(employeeSavDto.getId());
        employee.setRecommend(employeeSavDto.getRecommend());
        employee.setRecommendDescribe(employeeSavDto.getRecommendDescribe());
        employee.setLabelUuidList(employeeSavDto.getLabelUuidList());
        employeeService.saveEmployee(employee);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/getEmployeeReply", produces = { "text/html;charset=utf-8" })
    @ApiOperation(value = "经纪人自动回复规则", notes = "经纪人自动回复规则")
    public ResponseEntity<Object> getEmployeeReply() {
        String content = corporationService.getEmployeeReply(SecurityUtils.getCorporationCode());
        if(StringUtils.isNotBlank(content)) {
            content = content.replaceAll("\\{\\{公司名}}", "<span style=\"color: rgb(24, 144, 255);\">\\{\\{公司名}}</span>")
                    .replaceAll("\\{\\{门店}}","<span style=\"color: rgb(24, 144, 255);\">\\{\\{门店}}</span>")
                    .replaceAll("\\{\\{人名}}", "<span style=\"color: rgb(24, 144, 255);\">\\{\\{人名}}</span>")
                    .replaceAll("\\{\\{手机号}}", "<span style=\"color: rgb(24, 144, 255);\">\\{\\{手机号}}</span>")
                    .replaceAll("\\n", "<br/>");
        }
        return ResponseEntity.ok(content);
    }

    @PostMapping("/updateEmployeeReply")
    @ApiOperation(value = "编辑保存经纪人自动回复规则", notes = "编辑保存经纪人自动回复规则")
    public ResponseEntity<Object> updateEmployeeReply(@RequestBody EmployeeReplyEditDto employeeReplyEditDto) {
        try {
            corporationService.updateEmployeeReply(SecurityUtils.getCorporationCode(), employeeReplyEditDto.getRuleParams());
        } catch (Exception e) {
            throw new BusinessException("编辑经纪人自动回复规则异常");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/getAppointmentEmployee")
    @ApiOperation(value = "获取当前房源可指定经纪人", notes = "获取当前房源可指定经纪人")
    public ResponseEntity<Object> getAppointmentEmployee(@RequestBody AppointmentEmpAdminQueryDto appointmentEmpAdminQueryDto) {
        Corporation corporation = corporationService.getCorporationByCorporationCode(SecurityUtils.getCorporationCode());
        if (corporation != null && corporation.getV20Flag()) {
            return ResponseEntity.ok(appointmentService.getV20AdminEmpList(appointmentEmpAdminQueryDto));
        } else {
            return ResponseEntity.ok(appointmentService.getAdminEmpList(appointmentEmpAdminQueryDto));
        }
    }

}
